﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Business;
using STT.Entities;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace STT.Admin.UserControls
{
    public partial class ucBangDiem : System.Web.UI.UserControl
    {
        public bool chonSuaClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            chonSuaClick = false;
            LoadBangDiem();
           
                Loanddl();
            
        }
        void LoadBangDiem()
        {
            gvBangDiem.AutoGenerateColumns = false;
            gvBangDiem.DataSource = BTbBangDiem.SelectAll();
            gvBangDiem.DataBind();
        }
        void Loanddl()
        {
            ddlMaHV.DataSource = BTbHocVien.SelectAll();
            ddlTenHocVien.DataSource = BTbHocVien.SelectAll();
            ddlTenMonHoc.DataSource = BTbMonHoc.SelectAll();

            ddlMaHV.DataTextField = "MaHV";
            ddlTenHocVien.DataTextField = "TenHV";
            ddlTenMonHoc.DataTextField = "TenMonHoc";

            ddlTenMonHoc.DataValueField = "MaMonHoc";

            ddlMaHV.DataBind();
            ddlTenHocVien.DataBind();
            ddlTenMonHoc.DataBind();
        }
        protected void lbTimKiem_Click(object sender, EventArgs e)
        {

        }

        protected void gvBangDiem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
             if (e.CommandName == "ChonSua")
            {
                // Thay đổi tiêu đề
                txtTieuDe.InnerText = "Sửa khóa học";
                int index = Convert.ToInt32(e.CommandArgument);
                ddlMaHV.SelectedItem.Text = gvBangDiem.DataKeys[index].Value.ToString();
                ddlTenHocVien.SelectedItem.Text = gvBangDiem.Rows[index].Cells[2].Text;
                ddlTenMonHoc.SelectedItem.Text = gvBangDiem.Rows[index].Cells[3].Text;
                txtDTK.Text = gvBangDiem.Rows[index].Cells[4].Text;
                txtNamHoc.Text = gvBangDiem.Rows[index].Cells[5].Text;
                
                btSua.Visible = true;
                btThemMoi.Visible = false;
                chonSuaClick = true;
             }
        }

   

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
              BTbBangDiem.SearchMaHocVien(new ETbBangDiem
            {
                MaHV=  ddlMaHV.Text.ToString()
            });
            LoadBangDiem();
        }

        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }

        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
             try
            {
                for (int i = 0; i < gvBangDiem.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvBangDiem.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbKhoaHoc.Delete(int.Parse(gvBangDiem.DataKeys[i].Value.ToString()));
                    }
                }

                LoadBangDiem();
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến Khóa học này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void btThemMoi_Click(object sender, EventArgs e)
        {
             if (!KiemTraDuLieu())
            {
                BTbBangDiem.Insert(new ETbBangDiem
                {
                    MaHV  = ddlMaHV.Text,
                    MaMonHoc = Int32.Parse(ddlTenMonHoc.SelectedValue),
                      DiemTK = Double.Parse(txtDTK.Text),
                      Namhoc=txtNamHoc.Text
                    
                });

                LoadBangDiem();
            }
            else
            {
                ThongBao("Khóa học này đã tồn tại!");
                
            }
        }
         bool KiemTraDuLieu()
        {
            return BTbBangDiem.KiemTraDuLieu(new ETbBangDiem
            {
                MaHV = ddlMaHV.Text.ToString(),
               
                
            });
        }
        protected void btThemExcel_Click(object sender, EventArgs e)
        {

        }

        protected void gvBangDiem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBangDiem.PageIndex = e.NewPageIndex;
            gvBangDiem.DataBind();
        }
          void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void gvBangDiem_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvBangDiem.EditIndex = e.NewEditIndex;
            gvBangDiem.DataBind();
        }

        protected void gvBangDiem_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        try
            {
                int index = Convert.ToInt32(e.RowIndex);
                int MaMonHoc = int.Parse(gvBangDiem.DataKeys[index].Values[1].ToString());
                String MaHV = gvBangDiem.DataKeys[index].Values[0].ToString();

                BTbBangDiem.Delete(MaHV,MaMonHoc);
                LoadBangDiem();
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến bảng điểm  này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void gvBangDiem_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
         gvBangDiem.EditIndex = -1;
            gvBangDiem.DataBind();
        }
        protected void btSua_Click(object sender, EventArgs e)
        {
            BTbBangDiem.Update(new ETbBangDiem
            {
                MaHV  = ddlMaHV.Text,
                    MaMonHoc = Int32.Parse(ddlTenMonHoc.SelectedValue),
                      DiemTK = Double.Parse(txtDTK.Text),
                      Namhoc=txtNamHoc.Text
            });
            LoadBangDiem();
        }
    }
}


      

      
        
        
        




       
       

      

    

            
 